package com.probiz.estoresf.catalog.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.ProductFilterGroup;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;

public class ProductCategoryFrontController extends	GenericStoreFrontController<ProductCategory> {

	private ProductManager productManager = null;
	private ProductCategoryManager productCategoryManager = null;
	private ProdMainManager prodMainManager = null;

	protected void initController() throws Exception {
		mgr = productCategoryManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}

	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得商品目录------
		Integer categoryId = productCategoryManager.getCategoryIdByUri(request
				.getRequestURI());
		ProductCategory productCategory = productCategoryManager
				.getById(categoryId);
		// 没有找到相应目录不能访问,直接进入找不到或已删除页面
		if (productCategory == null || !productCategory.getStatus().equals(Constants.FLAG_TRUE)) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得商品目录------

		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);

		// ------start:改变页面显示条数 只有不是默认值的时候才需改变------
		if (searchCriteria.getPageSize() == defaultPageSize) {
			searchCriteria.changePaging(searchCriteria.getPageNo(), 20);
		}
		// ------end:改变页面显示条数 只有不是默认值的时候才需改变------

		// ------start:构造前台查询条件------
		ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria();
		
		bindOnly(request, productSearchCriteria, "productSearchCriteria");
		if(null != productCategory.getProductType()){
			productSearchCriteria = prodMainManager.setProductQuickSearchCriteria(productSearchCriteria, productCategory, request.getRequestURI());
		}	
		productSearchCriteria.setStatus(Product.STATUS_ACTIVE);
		if(null == productSearchCriteria.getOrderby()){
			productSearchCriteria.setOrderby((short)0);
		}
		productSearchCriteria.setCategoryPaths(new String[]{productCategory.getCategoryPath()});
		// ------end:构造前台查询条件------
		List<Product> productList = productManager
				.searchByCriteriaForController(productSearchCriteria,
						searchCriteria);

		ModelAndView mv = new ModelAndView("product/defaultProductList");
		mv.addObject("productList", productList);
		mv.addObject("category", productCategory);
		mv.addObject("productSearchCriteria", productSearchCriteria);

		// ------start:把页面list的显示风格设置到attribute中------
		Cookie[] cookies = request.getCookies();
		if (!ArrayUtils.isEmpty(cookies)) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("listShowStyle")) {
					mv.addObject("listShowStyle", cookie.getValue());
				}
				if (cookie.getName().equals("compareCartStyle")) {
					mv.addObject("compareCartStyle", cookie.getValue());
				}
			}
		}
		// ------end:把页面list的显示风格设置到attribute中------
		
		
		// ------start:设置快速搜索条件------
		if(null != productCategory.getProductType()){
			List<ProductFilterGroup> productFilterGroupList = prodMainManager.generateProductFilters(productCategory, request.getRequestURI(),request.getQueryString());
			//------start:将filter分类------
			List<List<ProductFilterGroup>>  productFilterGroupLists = new ArrayList<List<ProductFilterGroup>>();
			List<ProductFilterGroup> normalTypeList = new ArrayList<ProductFilterGroup>();
			List<ProductFilterGroup> optionValueTypeList = new ArrayList<ProductFilterGroup>();
			for(ProductFilterGroup group:productFilterGroupList){
				if(group.getType() == ProductFilterGroup.TYPE_NORMAL){
					normalTypeList.add(group);
				}else if(group.getType() == ProductFilterGroup.TYPE_SKUOPTIONVALUE){
					optionValueTypeList.add(group);
				}
			}
			productFilterGroupLists.add(normalTypeList);
			productFilterGroupLists.add(optionValueTypeList);
			
			//------end:  将filter分类------
			mv.addObject("productFilterGroupLists",productFilterGroupLists);
		}	
		// ------end:设置快速搜索条件------
		
		// ------start:获得对比车上的商品------
		List<Product> compareProductList = prodMainManager.getCompareProductsFromRequest(request);
		mv.addObject("compareProductList",compareProductList);
		// ------end:  获得对比车上的商品------
		return mv;

	}

}
